package com.hyfrogx.modules.app.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.Map;

/**
 * 输出字符串的工具类
 */
public class StrUtils {

	/**
	 * 获取集合中key的值，组成字符串
	 *
	 * @param list
	 * @param key
	 * @param chat
	 * @return
	 */
	public static String createGoodsName(List list, String key, String chat) {
		List<String> strings = CollUtil.newArrayList();
		if (CollUtil.isNotEmpty(list) && StrUtil.isNotEmpty(key)) {
			List names = CollUtil.getFieldValues(list, key);
			if (CollUtil.isNotEmpty(names)) {
				Map countMap = CollUtil.countMap(names);
				for (Object _key : countMap.keySet()) {
					strings.add(StrUtil.format("{}×{}", _key, countMap.get(_key)));
				}
			}
		}
		if (StrUtil.isNotEmpty(chat)) {
			chat = ";";
		}
		return CollUtil.join(strings, chat);
	}
}
